package com.Test.com;

import java.io.IOException;
import java.net.*;
import java.util.ArrayList;

public class ServeBordcast implements Runnable {
    private ArrayList<String> allLocalIp;
    private String serveMachineIp;

    public ServeBordcast(ArrayList<String> allLocalIp, String serveMachineIp) {
        this.allLocalIp = allLocalIp;
        this.serveMachineIp = serveMachineIp;
    }

    @Override
    public void run() {
        while (true) {
            for (int i = 0; i < allLocalIp.size(); i++) {
                DatagramSocket ds = null;
                try {
                    ds = new DatagramSocket();
                } catch (SocketException e) {
                }
                DatagramPacket dp = null;
                try {
                    dp = new DatagramPacket(serveMachineIp.getBytes(), serveMachineIp.getBytes().length, InetAddress.getByName(allLocalIp.get(i)), 5050);
                } catch (UnknownHostException e) {
                }
                try {
                    ds.send(dp);
                } catch (IOException e) {
                }
                ds.close();
            }
            System.out.println("广播服务器ip成功!");
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
            }

        }

    }
}
